package com.atguigu04.threadsafemore.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * ClassName: WindowsTest2
 * Package: com.atguigu04.threadsafemore.lock
 * Description:
 *
 * @Author BuTang
 * @Create 2023/5/16 14:56
 * @Version 1.0
 */
public class WindowsTest2 {
    public static void main(String[] args) {
        Windows2 a = new Windows2();

        Thread b = new Thread(a);
        Thread c = new Thread(a);


        b.start();
        c.start();

    }
}

class Windows2 implements Runnable {
    private static final ReentrantLock lock = new ReentrantLock();
    private static int ticket = 100;

    @Override
    public void run() {

        while (ticket > 0) {
            show();
        }
    }

    public void show() {
        try {
            Thread.sleep(1000);
           // System.out.println(Thread.currentThread().getName() + "线程进入");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }


        synchronized (this) {
            lock.lock();


            if (ticket > 0) {
                System.out.println(Thread.currentThread().getName() + "--" + ticket);
                ticket--;


                lock.unlock();
            }
        }
    }
}
